package com.example.springbootthymeleaf.controller;

import com.example.springbootthymeleaf.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Description:springbootweb
 * Created by admin  on 2020/4/16  13:18
 *
 *  页面跳转Controller
 */
@Controller
public class PageController {

    /**
     * 取值，循环
     * @return
     */
    @GetMapping(value = "/show")
    public String showPage(Model model){
        model.addAttribute("msg","Hello Thymeleaf");
        model.addAttribute("date",new Date());
        model.addAttribute("sex","男");
//        model.addAttribute("id","1");和下面的效果一样
        model.addAttribute("id",1);

        //List
        List<User> list = new ArrayList<User>();
        list.add(new User("1","老大",30));
        list.add(new User("2","老二",28));
        list.add(new User("3","老三",25));
        list.add(new User("4","老四",25));
        model.addAttribute("list",list);

        //Map
        Map<String,User> map= new HashMap<String,User>();
        map.put("user1",new User("1","老大",30));
        map.put("user2",new User("2","老二",30));
        map.put("user3",new User("3","老三",30));
        map.put("user4",new User("4","老四",30));
        model.addAttribute("map",map);
        return "index";
    }

    /**
     * 域对象
     * @param model
     * @return
     */
    @GetMapping("/get")
    public String getContext(Model model, HttpServletRequest request){
        model.addAttribute("id",1);
        model.addAttribute("name","张三");

        request.setAttribute("req","HttpServletRequest");
        request.getSession().setAttribute("ses","HttpSession");
        request.getSession().getServletContext().setAttribute("app","ApplicationContext");
        return "context";
    }

    @GetMapping(value = "/testUrl1")
    public String testUrl(Integer id,String name){
        System.out.println(id+"\t"+name);
        return "context";
    }

    @GetMapping(value = "/testRest1/{id}")
    public String testRest1(@PathVariable Integer id){
        System.out.println(id);
        return "context";
    }

    @GetMapping(value = "/testRest2/{id}/{name}")
    public String testRest2(@PathVariable Integer id, @PathVariable String name){
        System.out.println(id+"\t"+name);
        return "context";
    }

    @GetMapping(value = "/testRest3/{id}")
    public String testRest3(@PathVariable Integer id,String name){
        System.out.println(id+"\t"+name);
        return "context";
    }

    @GetMapping(value = "/show2")
    public String show2(){
        return "page/hello";
    }

}
